package service

import (
	"errors"
	"github.com/cn-ygf/go-admin/internal/modular/system/dao"
	"github.com/cn-ygf/go-admin/internal/modular/system/model"
	"github.com/cn-ygf/go-admin/internal/modular/system/vo"
)

var (
	PostService = new(postService)
)

var (
	PostNameUniqueError = errors.New("部门名称已存在")
)

// postService 部门管理
type postService struct {
}

func (s postService) GetList() ([]*model.SysPost, error) {
	return dao.SysPost.Where(dao.SysPost.Status.Eq("0")).Find()
}

// QueryList 分页查询部门列表
func (s postService) QueryList(req *vo.QueryPostListReq) ([]*model.SysPost, int64, error) {
	var (
		u = dao.SysPost
		d = u.Where()
	)
	if req.PageSize < 1 {
		req.PageSize = 10
	}
	if req.PageNum < 1 {
		req.PageNum = 1
	}
	if req.PostName != "" {
		d = d.Where(u.PostName.Like("%" + req.PostName + "%"))
	}
	if req.Status != "" {
		d = d.Where(u.Status.Eq(req.Status))
	}
	if req.PostCode != "" {
		d = d.Where(u.PostCode.Eq(req.PostCode))
	}
	return d.FindByPage((req.PageNum-1)*req.PageSize, req.PageSize)
}

// All 查询所有部门
func (s postService) All() ([]*model.SysPost, error) {
	return dao.SysPost.Find()
}

// GetByID 根据postId查询部门
func (s postService) GetByID(postId int64) (*model.SysPost, error) {
	return dao.SysPost.Where(dao.SysPost.PostID.Eq(postId)).First()
}

// GetByPostName 根据部门名查询
// postName 部门名
func (s postService) GetByPostName(postName string) (*model.SysPost, error) {
	return dao.SysPost.Where(dao.SysPost.PostName.Eq(postName)).First()
}

// ChangeStatus 修改部门状态
func (s postService) ChangeStatus(postId int64, status string) error {
	_, err := dao.SysPost.Where(dao.SysPost.PostID.Eq(postId)).Update(dao.SysPost.Status, status)
	return err
}

// Add 添加部门
func (s postService) Add(req *vo.AddPostReq) error {
	if !s.CheckPostNameUnique(0, req.PostName) {
		return PostNameUniqueError
	}
	u := dao.SysPost
	newPost := &model.SysPost{
		PostCode: req.PostCode,
		PostName: req.PostName,
		PostSort: req.PostSort,
		Status:   req.Status,
		CreateBy: req.CreateBy,
		Remark:   req.Remark,
	}
	err := u.Create(newPost)
	if err != nil {
		systemLog.Errorln(err.Error())
		return ServerError
	}
	return nil
}

// Update 修改部门
func (s postService) Update(req *vo.EditPostReq) error {
	u := dao.SysPost
	_, err := u.Where(u.PostID.Eq(req.PostId)).Updates(map[string]any{
		u.PostName.ColumnName().String(): req.PostName,
		u.PostCode.ColumnName().String(): req.PostCode,
		u.PostSort.ColumnName().String(): req.PostSort,
		u.Status.ColumnName().String():   req.Status,
		u.Remark.ColumnName().String():   req.Remark,
		u.UpdateBy.ColumnName().String(): req.UpdateBy,
	})
	if err != nil {
		systemLog.Errorln(err.Error())
		return ServerError
	}
	return nil
}

// Delete 删除部门
func (s postService) Delete(postIds []int64) error {
	_, err := dao.SysPost.Where(dao.SysPost.PostID.In(postIds...)).Delete()
	return err
}

// CheckPostNameUnique 检查岗位名称是否已存在
func (s postService) CheckPostNameUnique(postId int64, postName string) bool {
	if postId == 0 {
		count, err := dao.SysPost.Where(dao.SysPost.PostName.Eq(postName)).Count()
		if err != nil {
			systemLog.Errorln(err.Error())
			return true
		}
		return count < 1
	}
	post, err := s.GetByID(postId)
	if err != nil {
		systemLog.Errorln(err.Error())
		return true
	}
	if post.PostName == postName {
		return true
	}
	count, err := dao.SysPost.Where(dao.SysPost.PostName.Eq(postName)).Count()
	if err != nil {
		systemLog.Errorln(err.Error())
		return true
	}
	return count < 1
}

// CheckPostCodeUnique 检查岗位名称是否已存在
func (s postService) CheckPostCodeUnique(postId int64, postCode string) bool {
	if postId == 0 {
		count, err := dao.SysPost.Where(dao.SysPost.PostCode.Eq(postCode)).Count()
		if err != nil {
			systemLog.Errorln(err.Error())
			return true
		}
		return count < 1
	}
	post, err := s.GetByID(postId)
	if err != nil {
		systemLog.Errorln(err.Error())
		return true
	}
	if post.PostCode == postCode {
		return true
	}
	count, err := dao.SysPost.Where(dao.SysPost.PostCode.Eq(postCode)).Count()
	if err != nil {
		systemLog.Errorln(err.Error())
		return true
	}
	return count < 1
}
